package com.stadium.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.exception.RRException;
import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.StringUtils;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.*;
import com.stadium.entity.RecordStatistical;
import com.stadium.mapper.*;
import com.stadium.service.RecordStatisticalSV;
import com.stadium.vo.RecordStatisticalVo;
import com.stadium.vo.searchVo.BasicsRecordSearchVo;
import com.stadium.vo.searchVo.RecordDictSearchVo;
import com.stadium.vo.searchVo.RecordStatisticalSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RecordStatisticalSVI implements RecordStatisticalSV {
    @Autowired
    private RecordStatisticalMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private GameScheduleMP gameScheduleMP;
    @Autowired
    private BasicsRecordMP basicsRecordMP;
    @Autowired
    private GameEventMP gameEventMP;

    @Autowired
    private RecordDictMP recordDictMP;

    @Override
    public List<RecordStatistical> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, RecordStatistical.class);
    }

    @Override
    public void save(RecordStatisticalVo recordStatisticalVo) {
        RecordStatistical recordStatistical = new RecordStatistical();
        BeanUtils.copyProperties(recordStatisticalVo, recordStatistical);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        recordStatistical.setCreateUser(UserUtils.getUserId());
        recordStatistical.setUpdateUser(UserUtils.getUserId());
        recordStatistical.setId(id);
        mapper.save(recordStatistical);
    }

    @Override
    public void update(RecordStatisticalVo recordStatisticalVo) {
        RecordStatistical recordStatistical = new RecordStatistical();
        BeanUtils.copyProperties(recordStatisticalVo, recordStatistical);
        recordStatistical.setUpdateUser(UserUtils.getUserId());
        mapper.update(recordStatistical);
    }

    @Override
    public void saveBatch(List<RecordStatistical> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    public List<RecordStatisticalDto> query(RecordStatisticalSearchVo searchVo) {
        return mapper.query(searchVo);
    }

    @Override
    public RecordStatisticalDto get(String id) {
        return mapper.get(id);
    }

    @Override
    public int getCount(RecordStatisticalSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public Map<String,List<Map<String,Object>>> getInfo(String scheduleId,String belong) {
        GameScheduleDto gameScheduleDto = gameScheduleMP.get(scheduleId);
        if (gameScheduleDto == null) {
            throw new RRException("比赛不存在");
        }
        Map<String,List<Map<String,Object>>> resulte = new HashMap();
        if (StringUtils.isNotEmpty(belong)) {
            if ("0".equals(belong)) {

                RecordStatisticalSearchVo recordStatisticalSearchVo = new RecordStatisticalSearchVo();
                recordStatisticalSearchVo.setEventId(gameScheduleDto.getEventId());
                recordStatisticalSearchVo.setScheduleId(scheduleId);
                recordStatisticalSearchVo.setTeamId(gameScheduleDto.getHometeamId());
                recordStatisticalSearchVo.setBelong(belong);
                recordStatisticalSearchVo.setDeleteCode("0");
                recordStatisticalSearchVo.setUserId("0");
                List<Map<String,Object>> homeInfo = mapper.getInfo(recordStatisticalSearchVo);
                resulte.put(gameScheduleDto.getHometeamName(),homeInfo);
                recordStatisticalSearchVo.setEventId(gameScheduleDto.getEventId());
                recordStatisticalSearchVo.setScheduleId(scheduleId);
                recordStatisticalSearchVo.setTeamId(gameScheduleDto.getGuestteamId());
                recordStatisticalSearchVo.setBelong(belong);
                recordStatisticalSearchVo.setDeleteCode("0");
                recordStatisticalSearchVo.setUserId("0");
                List<Map<String,Object>> guestInfo = mapper.getInfo(recordStatisticalSearchVo);
                resulte.put(gameScheduleDto.getGuestteamName(),guestInfo);
            }
            if ("1".equals(belong)) {
                RecordStatisticalSearchVo recordStatisticalSearchVo = new RecordStatisticalSearchVo();
                recordStatisticalSearchVo.setEventId(gameScheduleDto.getEventId());
                recordStatisticalSearchVo.setScheduleId(scheduleId);
                recordStatisticalSearchVo.setTeamId(gameScheduleDto.getHometeamId());
                recordStatisticalSearchVo.setBelong(belong);
                recordStatisticalSearchVo.setDeleteCode("0");
                recordStatisticalSearchVo.setUserId(null);
                List<Map<String,Object>> homeInfo = mapper.getInfo(recordStatisticalSearchVo);
                resulte.put(gameScheduleDto.getHometeamName(),homeInfo);
                recordStatisticalSearchVo.setEventId(gameScheduleDto.getEventId());
                recordStatisticalSearchVo.setScheduleId(scheduleId);
                recordStatisticalSearchVo.setTeamId(gameScheduleDto.getGuestteamId());
                recordStatisticalSearchVo.setBelong(belong);
                recordStatisticalSearchVo.setDeleteCode("0");
                recordStatisticalSearchVo.setUserId(null);
                List<Map<String,Object>> guestInfo = mapper.getInfo(recordStatisticalSearchVo);
                resulte.put(gameScheduleDto.getGuestteamName(),guestInfo);
            }

        }
        return resulte;
    }

    @Override
    public Map<String, List<BasicsRecordDto>> record(String scheduleId) {
        GameScheduleDto gameScheduleDto = gameScheduleMP.get(scheduleId);
        if (gameScheduleDto == null) {
            throw new RRException("比赛不存在");
        }
        Map<String,List<BasicsRecordDto>> result = new HashMap();
        BasicsRecordSearchVo basicsRecordSearchVo = new BasicsRecordSearchVo();
        basicsRecordSearchVo.setEventId(gameScheduleDto.getEventId());
        basicsRecordSearchVo.setScheduleId(gameScheduleDto.getId());
        basicsRecordSearchVo.setTeamId(gameScheduleDto.getHometeamId());
        List<BasicsRecordDto> homeTeam = basicsRecordMP.query(basicsRecordSearchVo);
        result.put(gameScheduleDto.getHometeamName(), homeTeam);

        basicsRecordSearchVo.setEventId(gameScheduleDto.getEventId());
        basicsRecordSearchVo.setScheduleId(gameScheduleDto.getId());
        basicsRecordSearchVo.setTeamId(gameScheduleDto.getGuestteamId());
        List<BasicsRecordDto> guestteam = basicsRecordMP.query(basicsRecordSearchVo);
        result.put(gameScheduleDto.getGuestteamName(), guestteam);
        return result;
    }

    @Override
    public List<RecordDictDto> getHeader(String eventId, String belong) {
        GameEventDto gameEventDto = gameEventMP.get(eventId);
        if (gameEventDto == null) {
            throw new RRException("赛事不存在");
        }
        RecordDictSearchVo recordDictSearchVo = new RecordDictSearchVo();
        recordDictSearchVo.setCategoryId(gameEventDto.getCategoryId());
        recordDictSearchVo.setBelong(belong);
        recordDictSearchVo.setStatusCode(null);
        List<RecordDictDto> query = recordDictMP.query(recordDictSearchVo);
        return query;
    }

}
